package com.zhou.scaffolding.config;

import com.zhou.scaffolding.common.ErrorCode;
import com.zhou.scaffolding.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class CustomExceptionHandler {

    @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
    public Result handleException(HttpRequestMethodNotSupportedException e){
        log.error(e.getMessage(),e);
        return Result.success(ErrorCode.METHOD_NOT_SUPPORTED);
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Result notFount(RuntimeException e) {
        log.error("运行时异常:", e);
        return Result.success(ErrorCode.RUNTIME_ERROR);
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.success(ErrorCode.ERROR);
    }
}
